layui.use(['table', 'HttpRequest', 'func', 'form', 'laydate', 'gunsSelect'], function () {
    var $ = layui.$;
    var table = layui.table;
    var HttpRequest = layui.HttpRequest;
    var func = layui.func;
    var form = layui.form;
    var laydate = layui.laydate;
	var organizationXmSel;
    var gunsSelect = layui.gunsSelect;

    gunsSelect.render({
        url: Feng.ctxPath + '/hrOrganization/list',
        elem: '#workDept',
        fields: {name: 'orgName', value: 'orgId'},
        where: {workDept: ''}
    });

	/*var workDeptList = new HttpRequest(Feng.ctxPath + "/dict/list", 'get', function () {}).set({dictTypeCode: ''}).start();*/
	
    /**
     * 初始化参数
     */
    var Works = {
        tableId: "worksTable"
    };

    /**
     * 初始化表格的列
     */
    Works.initColumn = function () {
        return [[
            //{type: 'radio'},
			{field: 'workId', hide: true, title: '作品编号'},
            {
                field: 'workDeptName',  align: "center", title: '所属组织'
            },
			{field: 'workTitle',  align: "center", title: '作品标题'},
			{field: 'workDesc',  align: "center", title: '作品描述'},
			//{field: 'workImg',  align: "center", title: '作品缩略图'},
			{
                title: '作品缩略图', templet: function (d) {
                    var url = d.workImgUrl
                    return '<img data-index="' + (d.LAY_TABLE_INDEX) + '" src="' + url + '" class="tb-img-circle" tb-img alt="点击预览"/>';
                }, align: 'center', unresize: true
            },
			//{field: 'workUrl',  align: "center", title: '作品位置'},
			{
                title: '演讲作品', templet: function (d) {
                    var url = d.workVideoUrl
                    return `<button work-video type="button" class="layui-btn layui-btn-sm" src="${url}" >预览视频</button>`;
                }, align: 'center', unresize: true
            },
			{field: 'workAuthor',  align: "center", title: '作者'},
			{field: 'workSort', sort: true, align: "center", title: '作品排序'},
			{field: 'voteNum', sort: true, align: "center", title: '投票数'},
			{field: 'createdTime',  align: "center", title: '创建时间'},
            {align: 'center', toolbar: '#tableBar', title: '操作'}
        ]];
    };
	/* 点击图片放大 */
    $(document).off('click.tbImg').on('click.tbImg', '[tb-img]', function () {
        var imgList = table.cache[Works.tableId].map(function (d) {
            return {
                   src: d.workImgUrl
            }
        });
        layer.photos({photos: {data: imgList, start: $(this).data('index')}, shade: .1, closeBtn: true});
    });
	/* 点击图片放大 */
    $(document).on('click', '[work-video]', function () {
		var src = $(this).attr('src')
       	console.log(src)
		//var videoHtml = `<video style="max-width: 100%; max-height: 100%; display: block; margin: 0 auto" controls="controls" src="${src}"></video>`
		var videoHtml = `<live-player id='play' 
				video-url="${src}" 
				live="false" aspect="fullscreen"></live-player>`
		layer.open({
           type: 1
           ,offset: 'auto'
           ,area: ['80%', '80%']
           ,shadeClose: true//点击外围关闭弹窗
           ,scrollbar: true//不现实滚动条
           ,title: false //不显示标题
		   ,shade: .1
		   , closeBtn: true
           ,content: videoHtml //捕获的元素，注意：最好该指定的元素要存放在body最外层，否则可能被其它的相对元素所影响
           ,cancel: function () {
               
           }
       });
    });

    /**
     * 点击查询按钮
     */
    Works.search = function () {
        var queryData = {};
        queryData['workDept'] = $("#workDept").val();
        queryData['workTitle'] = $("#workTitle").val();
        queryData['workDesc'] = $("#workDesc").val();
        queryData['workImg'] = $("#workImg").val();
        queryData['workUrl'] = $("#workUrl").val();
        queryData['workAuthor'] = $("#workAuthor").val();
        queryData['workSort'] = $("#workSort").val();
        queryData['voteNum'] = $("#voteNum").val();
        queryData['createdBy'] = $("#createdBy").val();
        queryData['createdTime'] = $("#createdTime").val();
        queryData['updatedBy'] = $("#updatedBy").val();
        queryData['updatedTime'] = $("#updatedTime").val();
        table.reload(Works.tableId, {
            where: queryData, page: {curr: 1}
        });
    };

    /**
     * 添加对话框
     */
    Works.openAddDlg = function () {
        func.open({
            title: '添加',
            content: Feng.ctxPath + '/view/works/add',
            tableId: Works.tableId
        });
    };

    /**
     * 编辑对话框
     *
     * @param data 点击按钮时候的行数据
     */
    Works.openEditDlg = function (data) {
        func.open({
            title: '修改',
			content: Feng.ctxPath + '/view/works/edit?workId=' + data.workId,
            tableId: Works.tableId
        });
    };

    /**
     * 点击删除
     *
     * @param data 点击按钮时候的行数据
     */
    Works.onDeleteItem = function (data) {
        var operation = function () {
            var request = new HttpRequest(Feng.ctxPath + "/works/delete", 'post', function (data) {
                Feng.success("删除成功!");
                table.reload(Works.tableId);
            }, function (data) {
                Feng.error("删除失败!" + data.message + "!");
            });
			request.set("workId", data.workId);
            request.start(true);
        };
        Feng.confirm("是否删除?", operation);
    };

    // 渲染表格
    var tableResult = table.render({
        title: '演讲作品',
        elem: '#' + Works.tableId,
        toolbar: true,
        defaultToolbar: ['filter', 'print','exports'],
        url: Feng.ctxPath + '/works/page',
        page: true,
        height: "full-158",
		even: true,
        cellMinWidth: 100,
        cols: Works.initColumn(),
        request: {pageName: 'pageNo', limitName: 'pageSize'},
        parseData: Feng.parseData
    });

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        Works.search();
    });

    // 添加按钮点击事件
    $('#btnAdd').click(function () {
        Works.openAddDlg();
    });

    // 工具条点击事件
    table.on('tool(' + Works.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'edit') {
            Works.openEditDlg(data);
        } else if (layEvent === 'delete') {
            Works.onDeleteItem(data);
        }
    });

});